package com.ywk.rabbitmqCons.base.receiver;

import com.google.gson.Gson;
import com.ywk.rabbitmqCons.base.config.MQConfig;
import com.ywk.rabbitmqCons.business.message.BusinessMessage;
import com.ywk.rabbitmqCons.business.service.BusinessService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MQReceiver {
    private static Logger log=LoggerFactory.getLogger(MQReceiver.class);

    @Autowired
    BusinessService businessService;

    @RabbitListener(queues = MQConfig.QUEUE)
    public void receive(String message){
        log.info("receive message："+message);
    }

    @RabbitListener(queues = MQConfig.DIRECT_QUEUE1)
    public void receiveDirect1(Object message) {
        String msg=message.toString();
        log.info("receiveDirect1 message:"+msg);
    }

    @RabbitListener(queues = MQConfig.DIRECT_QUEUE2)
    public void receiveDirect2(Object message) {
        String msg=message.toString();
        log.info("receiveDirect2 message:"+msg);
    }

    @RabbitListener(queues = MQConfig.FANOUT_QUEUE1)
    public void receiveFanout1(String message){
        log.info("receiveFanout1 message："+message);
    }

    @RabbitListener(queues=MQConfig.FANOUT_QUEUE2)
    public void receiveFanout2(String message) {
        log.info("receiveFanout2 message:"+message);
    }


    @RabbitListener(queues = MQConfig.TOPIC_QUEUE1)
    public void receiveTopic1(String message){
        log.info("receiveTopic1 message："+message);
    }

    @RabbitListener(queues=MQConfig.TOPIC_QUEUE2)
    public void receiveTopic2(String message) {
        log.info("receiveTopic2 message:"+message);
    }

    @RabbitListener(queues=MQConfig.HEADER_QUEUE)
    public void receiveHeaderQueue(byte[] message) {
        log.info(" header  queue message:"+new String(message));
    }

    @RabbitListener(queues = MQConfig.BUSINESS_QUEUE)
    public void businessReceive(String message){
        log.info("businessReceive message："+message);
        Gson gson = new Gson();
        BusinessMessage businessMessage = gson.fromJson(message, BusinessMessage.class);
        businessService.consume(businessMessage);
    }
}
